package com.itheima.Tesk;

import java.util.*;

public class MyThread extends Thread {
    private static final int[] arr = {10, 5, 20, 50, 100, 200, 500, 800, 2, 80, 300, 700};
    private static final List<Integer> pool = new ArrayList<>();
    private static  int max;

    static {
        for (int i : arr) {
            pool.add(i);
        }
        Integer max = Collections.max(pool);
    }

    @Override
    public void run() {
        ArrayList<Integer> list = new ArrayList<>();
        Random r = new Random();
        Thread thread = Thread.currentThread();
        while (true) {
            synchronized (this) {
                if (pool.isEmpty()) break;
                int i = r.nextInt(pool.size());
                Integer remove = pool.remove(i);
                list.add(remove);
            }
        }
        System.out.println("在此次抽奖过程中，" + thread.getName() + "总共产生了" + list.size() + "个奖项，" +
                "分别为：" + list + "最大值：" + getMax(list) + "总和为" + getsum(list));
        if (getMax(list) == max) {
            System.out.println("在此次抽奖过程中," + thread.getName() + "中产生了最大奖项,该奖项金额为" +max);
        }
    }



    private int getsum(ArrayList<Integer> list) {
        int sum = 0;
        for (Integer integer : list) {
            if (list.isEmpty()) break;
            sum += integer;
        }
        return sum;
    }

    public int getMax(ArrayList<Integer> list) {
        int max = 0;
        for (Integer integer : list) {
            if (list.isEmpty()) break;
            if (max < integer) {
                max = integer;
            }
        }
        return max;
    }

}
